package edu.office.applicationRepository;

import edu.office.entities.*;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.util.*;

@Named("wrongquestionCollectionRepositoryA")
@ApplicationScoped
public class WrongquestionCollectionRepository
        extends ApplicationCommonRepository<WrongquestionCollection>  {
    @Inject
    SchoolRepository schoolRepository;
    @Inject
    QuestionRepository questionRepository;
    @Inject
    KnowledgeRepository knowledgeRepository;
    @Inject
     StudentRepository applicationStudentController;
    @Inject
     WrongquestionCollectionRepository applicationWrongquestionCollectionController;
      @Inject
    TeacschoolsubjectRepository teacschoolsubjectRepository;
    public WrongquestionCollectionRepository() {
    }

    public List<WrongquestionCollection> getWrongquestionCollection4Student(Integer studentid) {
        return find("STUDENT_ID",studentid).list();
    }
/*
    public List<Qwquestionwrong> refreshWrongQuestion(Subject subject, TeacherAdmin teacherAdmin, Student student, Parent parent, School school) {
        Set<WrongquestionCollection> temWrongquestions = new HashSet<>();
        Teacschoolsubject accurateSchoolSubject = null;
        if (null != teacherAdmin) {//老师选中了一门课程
            Set<Teacschoolsubject> teacschoolsubjectSet =teacschoolsubjectRepository.getTeacschoolsubjects4Teacher(teacherAdmin);

            if (school.getId() != null) {//班级也确定了
                for (Teacschoolsubject teaSchoSub : teacschoolsubjectSet) {
                    if (teaSchoSub.getSchoolid().equals(school) && teaSchoSub.getSubjectid().equals(subject)) {
                        accurateSchoolSubject = teaSchoSub;
                        break;
                    }
                }
            }
            applicationStudentController.list("SCHOOL_ID", accurateSchoolSubject.getSchoolid()).forEach(temstudent -> {//本班的学生
                applicationWrongquestionCollectionController.list("STUDENT_ID", temstudent.getId()).forEach(wrongquestion -> {
                    temWrongquestions.add(wrongquestion);
                });
            });
        } else if (null != student && null == parent) {
            applicationWrongquestionCollectionController.list("STUDENT_ID", student.getId()).forEach(wrongquestion -> {
                temWrongquestions.add(wrongquestion);
            });
        } else if (null != parent && student != null) {//双亲选中了一个孩子
            applicationWrongquestionCollectionController.list("STUDENT_ID", student.getId()).forEach(wrongquestion -> {
                temWrongquestions.add(wrongquestion);
            });
        }
        List<WrongquestionCollection> wrongQuestionCollectionList = new LinkedList<>();
        HashMap<Question, List<WrongquestionCollection>> questionWrongMap = new HashMap<>();

        //下面排除不是本课程的题目
        temWrongquestions.forEach(wrq -> {
            Question q = questionRepository.find("ID", wrq.getQuestionId()).firstResult();
            Knowledge k = knowledgeRepository.find("ID", q.getKnowledgeId()).firstResult();
            if (k.getSubjectId().equals(subject.getId())) {
                wrongQuestionCollectionList.add(wrq);
            }
        });
        temWrongquestions.clear();
        //下面获得题目的id号，再通过这些id号，到视图中获得题目与错原因
        StringBuilder sb = new StringBuilder();
        wrongQuestionCollectionList.forEach(wq -> {
            sb.append(wq.getId()).append(",");
        });
        List<Qwquestionwrong> qwquestionwrongs = new LinkedList<>();
        if (sb.length() > 0) {//有题目存在，下面查找视图中符合条件的记录
            String questionIds = sb.substring(0, sb.length() - 1);
            qwquestionwrongs = applicationQuestionwrongController.list(
                    "select * from Qwquestionwrong where qid in (" + questionIds + ")");
        }
        return qwquestionwrongs;
    }*/
}
